<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true">
      <el-form-item label="计算机名" prop="skName">
        <el-input
          v-model="queryParams.skName"
          placeholder="请输入计算机名"
          clearable
          style="width: 100%"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="操作系统类型"
                    prop="skType">
        <el-select v-model="queryParams.skType"
                   clearable
                   style="width:100%">
          <el-option v-for="item in typeOptions"
                     :key="item.value"
                     :value="item.value"
                     :label="item.value"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="连接状态" prop="isOnline">
        <el-select v-model="queryParams.isOnline" clearable style="width: 100%">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
          <!-- <el-option :label="'在线'" :value="0"></el-option>
          <el-option :label="'离线'" :value="1"></el-option>-->
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10"
            class="mb8">
      <right-toolbar :show-search.sync="showSearch"
                     :columns="columns"
                     @queryTable="getList" />
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="severMangeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="计算机名"
        align="center"
        prop="skName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="ip"
        align="center"
        prop="skIp"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="连接状态"
        align="center"
        prop="isOnline"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <template v-if="scope.row.isOnline == '1'">
            <el-tag type="success">在线</el-tag>
          </template>
          <template v-else>
            <el-tag type="danger">离线</el-tag>
          </template>
          <!-- <span>{{ scope.row.isOnline == "1" ? "在线" : "离线" }}</span> -->
        </template>
        <!-- <el-option :label="'在线'" :value="0"></el-option>
        <el-option :label="'离线'" :value="1"></el-option>-->
      </el-table-column>
      <el-table-column
        label="操作系统类型"
        align="center"
        prop="skType"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="300px"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:dict:edit']"
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="sourceStatus(scope.row)"
            >资源状态</el-button
          >
          <el-button
            v-hasPermi="['system:dict:edit']"
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="viewSimulationList(scope.row)"
            >模拟器列表</el-button
          >
          <el-button
            v-hasPermi="['system:dict:edit']"
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="viewSoftwareList(scope.row)"
            >软件列表</el-button
          >
          <!-- <el-button v-hasPermi="['system:dict:edit']"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="hangup(scope.row)">挂起</el-button>
          <el-button v-hasPermi="['system:dict:edit']"
                     size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="handleRemove(scope.row)">删除</el-button> -->
          <el-button
            v-hasPermi="['system:dict:edit']"
            :disabled="scope.row.isOnline == '1'"
            size="mini"
            type="text"
            @click="handleConntect(scope.row)"
          >
            <i
              class="el-icon-share"
              :id="scope.row.skId"
              style="margin-right: 1px"
            ></i
            >连接</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <source-status ref="sourceStatus"></source-status>
    <MonitoringDialog ref="monitoringDialogRef" />
  </div>
</template>

<script>
import { severMangeList, handConnect } from "@/api/resource/sever-mange";
import SourceStatus from "./components/source-status.vue";
import MonitoringDialog from "@/components/MonitoringDialog";
export default {
  components: { SourceStatus, MonitoringDialog },
  data() {
    return {
      typeOptions: [
        { value: "centos" },
        { value: "windows" },
        { value: "ubuntu" },
      ],
      statusOptions: [
        {
          value: "0",
          label: "离线",
        },
        {
          value: "1",
          label: "在线",
        },
      ],

      // 弹出层标题
      title: "",
      deployTitle: "",
      // 是否显示弹出层
      open: false,
      partOpen: false,
      // 遮罩层
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        skName: void 0,
        isOnline: void 0,
      },
      // 表格数据
      severMangeList: [],
      //定时器
      refresh: void 0,
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  mounted() {
    this.refresh = setInterval(() => {
      this.getList();
    }, 15000);
  },
  destroyed() {
    clearInterval(this.refresh);
  },
  methods: {
    // 连接
    handleConntect(row) {
      const icon = document.getElementById(row.skId);
      this.$confirm("确定要手动连接吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          icon.className = "el-icon-loading";
          handConnect({ skId: row.skId }).then((res) => {
            if (res.code == 200) {
              icon.className = "el-icon-share";
              this.$message.success("连接成功");
            } else {
              icon.className = "el-icon-share";
              this.$message.error(res.msg);
            }
            this.getList();
          });
        })
        .catch(() => {});
    },
    // 资源状态按钮
    sourceStatus(row) {
      // this.$refs.sourceStatus.open(row);
      this.$refs.monitoringDialogRef.open(row);
      console.log("222222", 2323232);
    },
    reset() {},
    async getList() {
      this.loading = true;
      severMangeList(this.queryParams).then((response) => {
        this.severMangeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        skName: void 0,
        isOnline: void 0,
      }),
        this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 软件部署按钮操作 */
    softwareDeploy(row) {
      let param = Buffer.from(
        JSON.stringify({
          skId: row.skId,
          skIp: row.skIp,
          skName: encodeURI(row.skName),
          skType: row.skType,
          isOnline: row.isOnline,
          os: row.os,
          ws: row.ws,
        })
      ).toString("base64");
      if (this.$store.state.permission.deployFlag) {
        if (this.$store.state.permission.skID == row.skId) {
          // 点击正在部署的受控 跳转受控页面
          this.$router.push(`/resource/severMange/deploy?p=${param}`);
        } else {
          this.$message.warning("有受控端正在部署，请等待部署完成");
        }
        return;
      }
      if (row.isOnline == "0") {
        this.$notify({
          title: "提示",
          // message: "受控端计算机处于离线状态",
          dangerouslyUseHTMLString: true,
          message: `<div>受控IP:${row.skIp}</div><div>受控主机名:${row.skName}</div>处于离线状态`,
          type: "warning",
        });
      }

      this.$router.push(`/resource/severMange/deploy?p=${param}`);
    },
    // 查看模拟器列表
    viewSimulationList(row) {
      let param = Buffer.from(
        JSON.stringify({
          skId: row.skId,
          skIp: row.skIp,
          skName: encodeURI(row.skName),
          skType: row.skType,
          isOnline: row.isOnline,
          os: row.os,
          ws: row.ws,
        })
      ).toString("base64");
      this.$router.push(`/resource/simulation/detail/list?p=${param}`);
    },
    // 查看软件列表
    viewSoftwareList(row) {
      let param = Buffer.from(
        JSON.stringify({
          skId: row.skId,
          skIp: row.skIp,
          skName: encodeURI(row.skName),
          skType: row.skType,
          isOnline: row.isOnline,
          os: row.os,
          ws: row.ws,
        })
      ).toString("base64");
      this.$router.push(`/resource/software/detail/list?p=${param}`);
    },
    getCurrentRow(row) {
      console.log(row);
    },
  },
};
</script>

<style lang="scss" scoped></style>
